import axios from 'axios'
import { put, takeEvery } from 'redux-saga/effects'
import { API } from '../../config'
import {
  GET_CATEGORY_LIST,
  GET_CATEGORY_LIST_SUCCESS,
} from '../actions/category'
import { Category } from '../models/category'

function* handleGetCategoryList() {
  const response: { data: Category[] } = yield axios.get<Category[]>(
    `${API}/categories`
  )
  yield put({ type: GET_CATEGORY_LIST_SUCCESS, payload: response.data })
}

export default function* categorySaga() {
  yield takeEvery(GET_CATEGORY_LIST, handleGetCategoryList)
}
